
export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = Math.random() * 16 | 0,
      v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  }).replaceAll(/-/g, '');
}

export function isNumber(value) {
  return typeof value === 'number' && !isNaN(value);
}

export function isFunction(value) {
  return typeof value === 'function';
}

export function readableFileSize(bytes) {
  bytes = parseInt(bytes);
  const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  let unitIndex = 0;

  while (bytes >= 1024 && unitIndex < units.length - 1) {
    bytes /= 1024;
    unitIndex++;
  }

  // 保留两位小数
  return bytes.toFixed(2) + " " + units[unitIndex];
}

export function createProps (type, defaultValue) {
  if (type === String) return { type: String, default: defaultValue || undefined }
  if (type === Object) return { type: Object, default: defaultValue || (() => ({})) }
  if (type === Array) return { type: Array, default: defaultValue || (() => []) }
  if (type === Function) return { type: Function, default: defaultValue || undefined }
  if (type === Boolean) return { type: Boolean, default: defaultValue || false }
  if (type === Number) return { type: Number, default: defaultValue || 0 }
}

export class AntiJitter {
  timeout = 300
  timer = null

  constructor (callback = () => {
  }, timeout = 300) {
    this.timeout = timeout
    this.callback = callback
  }

  invoke () {
    if (this.timeout <= 0) {
      return this.callback()
    }
    if (this.timer) {
      clearTimeout(this.timer)
    }

    this.timer = setTimeout(() => this.callback(), this.timeout)
  }

  callback = () => {
  }
}
